package com.zt.controller;

import com.zt.entity.Result;
import com.zt.service.CoursesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zt.pojo.Courses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CourseController {

    @Autowired
    private CoursesService courseService;

    @GetMapping("/api/courses/recommend")
    public Result getRecommendCourses() {
        try {
            List<Map<String, Object>> recommendCourses = courseService.getRecommendCourses();
            return Result.success("cg",recommendCourses);
        } catch (Exception e) {
            return Result.error("获取推荐课程失败");
        }   
    }

    /**
     * 根据教师ID查询课程信息
     * @param teacherId 教师ID
     * @return 该教师的课程列表
     */
    @GetMapping("/api/courses/teacher")
    public Result getTeacherCourses(@RequestParam(value = "teacherId") Integer teacherId) {
        try {
            System.out.println("查询教师课程，教师ID: " + teacherId);
            
            if (teacherId == null || teacherId <= 0) {
                return Result.error("教师ID无效");
            }
            
            // 查询条件：根据教师ID筛选，且状态为正常(1)的课程
            LambdaQueryWrapper<Courses> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Courses::getTeacherId, teacherId)
                       .eq(Courses::getStatus, 1)
                       .orderByDesc(Courses::getCreatedAt); // 按创建时间降序排列，最新课程在前
            
            // 执行查询
            List<Courses> coursesList = courseService.list(queryWrapper);
            System.out.println("查询到课程数量: " + coursesList.size());
            
            // 如果没有课程数据，返回空列表
            if (coursesList.isEmpty()) {
                return Result.success("该教师暂无课程", new ArrayList<>());
            }
            
            // 转换为前端需要的格式
            List<Map<String, Object>> resultList = new ArrayList<>();
            for (Courses course : coursesList) {
                Map<String, Object> courseMap = new HashMap<>();
                courseMap.put("id", course.getId());
                courseMap.put("title", course.getTitle());
                courseMap.put("description", course.getDescription());
                courseMap.put("price", course.getPrice());
                courseMap.put("originalPrice", course.getOriginalPrice());
                courseMap.put("coverImage", course.getCoverImage());
                courseMap.put("subjectId", course.getSubjectId());
                courseMap.put("createdAt", course.getCreatedAt());
                // 添加其他可能需要的课程信息字段
                courseMap.put("teacherId", course.getTeacherId());
                
                resultList.add(courseMap);
            }
            
            return Result.success("获取教师课程成功", resultList);
        } catch (Exception e) {
            System.out.println("获取教师课程异常: " + e.getMessage());
            e.printStackTrace();
            return Result.error("获取教师课程失败: " + e.getMessage());
        }
    }




}